<?php

namespace Home\Lib;

use Think\Controller;

class ModuleInfo extends Controller
{
    // 获得所有模块的信息
    public function getModuleInfo()
    {
        $this->assign('type', I('get.type'));
        $this->fpage('module', 50, array('type' => I('get.type')));
        $this->assign('form', session('form'));
        $this->assign('username', session('user_name'));
        $this->display('moduleInfo');
    }

    // 添加模块
    public function addModule()
    {
        $this->assign('username', session('user_name'));
        $this->assign('form', session('form'));
        $this->assign("type", I("get.type"));
        $this->display('addModule');
    }

    public function addModuleHandler()
    {
        if (IS_POST && I('post.number')) {
            $number = I('post.number') ? I('post.number') : 0;
            if (I('post.type') == '交流专业') {
                if (strpos(I('post.number'), 'V') === false) {
                    $number = I('post.number').'V';
                }
            }
            $basenum = I('basenum') ? I('basenum') : 0;
            $data = array(
                'module_name' => I('post.module_name'),
                'number' => $number,
                'baseprice' => I('post.baseprice'),
                'safeprice' => I('post.safeprice'),
                'class' => I('post.class'),
                'type' => I('post.type'),
                'measure' => I('measure'),
                'basenum' => $basenum,
                'remark' => I('post.remark'),
            );
            if (M('module')->add($data)) {
                $this->success('模块已添加', U('Home/Module/moduleInfo', array('status' => '',"type"=>I('post.type'))), 1);
            } else {
                $this->error('添加失败', U('Home/Module/moduleInfo', array('status' => 'add')), 3);
            }
        } else {
            $this->error('添加失败', U('Home/Module/moduleInfo', array('status' => 'info')), 3);
        }
    }

    // 修改模块信息
    public function modModule()
    {
        $module = M('module');
        $moduleData = $module->where(array('id' => I('id')))->find();
        $this->assign('moduleData', $moduleData);
        $this->assign('type', array('请选择' => '请选择', '土建专业' => '土建专业', '铁塔专业' => '铁塔专业', '交流专业' => '交流专业', '配套专业' => '配套专业'));
        $this->assign('class', array('请选择' => '请选择', '新建' => '新建', '改造' => '改造'));
        $this->assign('username', session('user_name'));
        $this->assign('form', session('form'));
        $this->display('modModule');
    }

    public function modModuleHandler()
    {
        if (IS_POST) {
            $basenum = I('basenum') ? I('basenum') : 0;
            $data = array(
                'id' => I('post.id'),
                'module_name' => I('post.module_name'),
                'number' => I('post.number'),
                'baseprice' => I('post.baseprice'),
                'safeprice' => I('post.safeprice'),
                'class' => I('post.class'),
                'type' => I('post.type'),
                'measure' => I('measure'),
                'basenum' => $basenum,
                'remark' => I('post.remark'),
            );
            if (M('module')->save($data)) {
                $this->success('模块已修改', U('Home/Module/moduleInfo', array('status' => '',"type"=>I('post.type'))), 1);
            } else {
                $this->error('修改失败', U('Home/Module/moduleInfo', array('status' => 'mod', 'id' => I('id'))), 3);
            }
        }
    }

    // 删除模块
    public function delModule()
    {
        $module = M('module');
        $re=$module->where(array('id' => I('id')))->find();
        $discount = M('discount');
        $resdelModule = $module->where(array('id' => I('id')))->delete();
        $resdelDiscount = $discount->where(array('modid' => I('id')))->delete();
        if ($resdelModule || $resdelDiscount) {
            $this->success('模块已删除', U('Home/Module/moduleInfo', array('status' => '', 'type'=>$re["type"])), 1);
        } else {
            $this->error('删除失败', U('Home/Module/moduleInfo', array('status' => '', 'type'=>$re["type"])), 3);
        }
    }

    // 分页方法
    private function fpage($table, $listRows, $con, $group = '', $order = '')
    {
        $table = M($table);
        $count = $table->where($con)->count();
        $fpage = new \Think\Page($count, $listRows);
        $fpage->setConfig('header', '<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;
              第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
        $fpage->setConfig('first', '首页');
        $fpage->setConfig('prev', '上一页');
        $fpage->setConfig('next', '下一页');
        $fpage->setConfig('last', '末页');
        $fpage->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER% ');
        $fpage->rollPage = 5;
        $pageResult = $table->where($con)->group($group)->order($order)
                                ->limit($fpage->firstRow.','.$fpage->listRows)
                                ->select();
        $fpage->lastSuffix = false;
        $show = $fpage->show();
        $this->assign('count', $count);
        $this->assign('listRows', $fpage->listRows);
        $this->assign('moduleData', $pageResult);
        $this->assign('show', $show);
    }
}
